package com.iuv.shortmsgnotice.service.Impl;

import com.iuv.shortmsgnotice.domain.DemoSmsExpressTbl;
import com.iuv.shortmsgnotice.repository.DemoSmsExpressTblRepository;
import com.iuv.shortmsgnotice.service.DemoSmsExpressService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * DemoSmsExpressServiceImpl
 * 物流业务实现类
 *
 * @author Will
 * @date 15:17
 */
@Service("demoSmsExpressService")
public class DemoSmsExpressServiceImpl implements DemoSmsExpressService {


    @Autowired
    private DemoSmsExpressTblRepository demoSmsExpressTblRepository;

    @Override
    public Page<DemoSmsExpressTbl> listSearch(DemoSmsExpressTbl demoSmsExpressTbl, Pageable pageable) {
        Specification<DemoSmsExpressTbl> specification = new Specification<DemoSmsExpressTbl>() {
            @Override
            public Predicate toPredicate(Root<DemoSmsExpressTbl> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                //客户名模糊查询
                if (StringUtils.isNoneBlank(demoSmsExpressTbl.getCustomerName())) {
                    list.add(criteriaBuilder.like(root.get("customerName").as(String.class), "%" + demoSmsExpressTbl.getCustomerName() + "%"));
                }
                //电话号码模糊查询
                if (StringUtils.isNoneBlank(demoSmsExpressTbl.getMobile())) {
                    list.add(criteriaBuilder.like(root.get("mobile").as(String.class), "%" + demoSmsExpressTbl.getMobile() + "%"));
                }
                //取件码完全匹配
                if (StringUtils.isNoneBlank(demoSmsExpressTbl.getPickUpCode())) {
                    list.add(criteriaBuilder.equal(root.get("pickUpCode"), demoSmsExpressTbl.getPickUpCode()));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        };
        //分页查询
        return demoSmsExpressTblRepository.findAll(specification, pageable);
    }

    @Override
    public DemoSmsExpressTbl enterStore(String customerName, String mobile) {
        String pickUpCode = RandomStringUtils.randomAlphanumeric(6);
        String remark = "";
        //创建入库记录
        DemoSmsExpressTbl demoSmsExpressTbl = new DemoSmsExpressTbl(customerName, mobile, pickUpCode, remark);
        return demoSmsExpressTblRepository.save(demoSmsExpressTbl);
    }

    @Override
    public DemoSmsExpressTbl pickUp(Integer expressId) throws NullPointerException {
        DemoSmsExpressTbl demoSmsExpressTbl = demoSmsExpressTblRepository.findById(expressId).get();

        if (demoSmsExpressTbl == null) {
            /**
             * TODO 短信推广 问题四 [异常问题抛出 throw]
             * 程序发生意外时，应该通过 throw 抛出空异常
             */
            throw new NullPointerException("取件记录不存在");

            /**
             * end
             */
        } else {
            //过滤重复取件误操作
            if(demoSmsExpressTbl.getOutTime()==null) {
                //登录出库时间
                demoSmsExpressTbl.setOutTime(Calendar.getInstance().getTime());
                demoSmsExpressTblRepository.saveAndFlush(demoSmsExpressTbl);
            }
        }
        return demoSmsExpressTbl;
    }
}
